package com.dlc.felear.xiaoensale.utils;

import com.dlc.felear.xiaoensale.factory.OrderFactory;

public class SerialDataHandle {
    private static String TAG = "SerialDataHandle";
    private Callback callback;
    private byte[] headers;
    private byte[] buff = null;

    public SerialDataHandle(Callback callback) {
        this.callback = callback;
    }

    public void setHeaders(byte[] headers) {
        this.headers = headers;
    }

    public void parseData(byte[] orders) {
//        MyLog.e(TAG, " 串口信息=" + HexUtil.bytes2HexString(orders));
        try {
            if (checkHeader(orders) && OrderFactory.checkDataComplete(orders)) {
                if (orders.length > 5) {
                    sendData(orders, true);
//                    MyLog.e(TAG, " 完整数据 " + HexUtil.bytes2HexString(orders));
                    clearArr();
                }
                return;
            } else {
                combineData(orders);
            }
        } catch (Exception e) {
            sendData(orders, false);
        }

    }

    private void sendData(byte[] data, boolean success) {
        if (callback != null && success) {
            callback.callback(data, success);
        }
    }

    private void combineData(byte[] orders) {
        try {
            if (checkHeader(orders)) {
                buff = orders;
//                MyLog.e(TAG, " 符合帧头 " + HexUtil.bytes2HexString(orders));
            } else {
//                MyLog.e(TAG, " 不符合帧头 " + HexUtil.bytes2HexString(orders));
                if (buff != null) {
                    int size = buff.length + orders.length;
                    byte[] tr = new byte[size];
                    System.arraycopy(buff, 0, tr, 0, buff.length);
                    System.arraycopy(orders, 0, tr, buff.length, orders.length);
                    compressData(tr);
                }
                buff = null;
            }
        } catch (Exception e) {
            sendData(orders, false);
        }

    }

    private void compressData(byte[] orders) {
//        MyLog.e(TAG, " 完整数据长度=" + orders.length + " 合并后的信息=" + HexUtil.bytes2HexString(orders));
        boolean success = checkHeader(orders) && OrderFactory.checkDataComplete(orders);
//        MyLog.e(TAG, "校验合并后的数据 success=" + success);
        if (orders.length < 5) {
            sendData(orders, false);
        } else {
            sendData(orders, success);
        }
    }

    private void clearArr() {
        buff = null;
    }


    /**
     * 检查帧头
     *
     * @param order
     * @return
     */
    private boolean checkHeader(byte[] order) {
        boolean temp = true;
        try {
            if (headers == null || headers.length <= 0 || order.length <= headers.length) {
                return false;
            }
            for (int i = 0; i < headers.length; i++) {
                if (headers[i] != order[i]) {
                    return false;
                }
            }
        } catch (Exception e) {
            return false;
        }
        return temp;
    }

    public interface Callback {
        void callback(byte[] arr, boolean success);
    }
}
